package com.example.lovewordsapplication.ui.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.lovewordsapplication.MyApplication;
import com.example.lovewordsapplication.R;
import com.example.lovewordsapplication.base.BaseActivity;
import com.example.lovewordsapplication.entity.UserInfo;
import com.example.lovewordsapplication.utils.ErrorCodeUtil;
import com.example.lovewordsapplication.utils.KeyboardUtil;
import com.example.lovewordsapplication.utils.ToastUtil;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;

import java.util.Arrays;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 找回密码
 *
 * @author Leung
 * @date 2020/4/7 23:37
 */
public class ForgetActivity extends BaseActivity implements View.OnClickListener {

    private EditText etAccount;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_forget;
    }

    @Override
    protected String setTitle() {
        return "重置密码";
    }

    @Override
    protected boolean isShowBack() {
        return true;
    }

    @Override
    protected void initView() {
        etAccount = (EditText) findViewById(R.id.et_account);

        Button btnConfirm = (Button) findViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(this);

    }

    private void submit() {
        String account = etAccount.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            toast("账号不能为空");
            return;
        }
        // 通过邮箱修改密码
        BmobUser.resetPasswordByEmail(account, new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Intent intent = new Intent();
                    intent.putExtra("username", account);
                    setResult(RESULT_OK, intent);
                    AlertDialog builder = new MaterialAlertDialogBuilder(ForgetActivity.this)
                            .setTitle("密码重置")
                            .setMessage("重置密码请求成功，请到" + account + "邮箱进行密码重置操作!")
                            .setPositiveButton("确定", ((dialog, which) -> finish()))
                            .setCancelable(false)
                            .show();
                } else {
                    ErrorCodeUtil.getErrorMessage(e.getErrorCode(), MyApplication.getInstance().getContext());
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        submit();
    }
}
